<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Weixin\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class RouterController extends WeixinController {



    protected function _initialize(){
        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置
        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }
        $options = array(
            'token'     => $this->token,   // 填写你设定的key
            'appid'     => $this->appid,    // 填写高级调用功能的appid
            'appsecret' => $this->appsecret // 填写高级调用功能的密钥
        );
        $this->weixin = new \Common\Extend\Wechat\Wechat($options);
        // $this->createmenu();
    }
    public function rest(){
        $echoStr = isset($_GET["echostr"]) ? $_GET["echostr"]: '';
        if(!empty($echoStr)){
            $this->weixin->valid();
        }else{
            $this->message();
        }
    } //transfer_customer_service    多人客服
    protected function arraySearch($keyword,$array){
        foreach($array as $k => $v){// print_r($keyword);exit;
            if(strstr( $keyword,$v)){
                return true;break;
            }
        }
    }
    protected function message(){
        $weixin = $this->weixin;
        $type = $weixin->getRev()->getRevType();
        switch($type) {
          		case 'text':
                    $Content = $weixin->getRevContent();
                    if(strstr($Content,"r")){
                        $weixin->transfer_customer_service()->reply();
                    }else{
                         $volist = M('Weixinreply')->where('status=1')->order('type asc')->select();
                         if(!empty($volist)){
                         foreach($volist as $val){
                             if($val['type']==1){
                                 $keywords =  explode(',',$val['keywords']);
                                 if (in_array($Content, $keywords)){
                                     $weixin->text($val['content'])->reply();
                                     break;
                                 }
                             }elseif($val['type']==2){
                                 $keywords =  explode(',',$val['keywords']);
                                 if ($this->arraySearch($Content, $keywords)){;
                                     $weixin->text($val['content'])->reply();
                                     break;
                                 }
                             }else{
                                 $weixin->text($val['content'])->reply();
                                 break;
                             }
                         }
                         }else{

                         }
                      //weixinreply(){}
                     } 
           		break;
           		case 'event': 
                    $Event = $weixin->getRevEvent(); 
          					if($Event['event'] == 'subscribe'){
          					

          					}elseif($Event['key'] == 'APP_LOAD'){ 
                                    $weixin->text('正在开发中...')->reply();     
                                                                
                              }  

          					  
                     		break;
                     		case 'image':

                        	break;
                         		default:
                                  $weixin->text("help info")->reply();
                         }
    }

    public function createmenu(){ //echo U('Authentication/index','',true,'weixin.jcsmkj.cn');exit;
        $data = [
              'button'=>[

                  [
                      "type"=>"view",
                      "name"=>"商户控制台",
                      "url"=>U('Koubei/index','',true,'qnm.upslope.cn'),
                      
                  ],
                  [
                      "type"=>"view",
                      "name"=>"账单",
                      "url"=>"http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=index#0",
                  ],


                  [
                      "type"=>"view",
                      "name"=>"商户常见问题",
                      "url"=>U('Koubei/question','',true,'qnm.upslope.cn'),
                  ],
                  


                  

              ],
        ];


       $rel = $this->weixin->createMenu($data);
      print_r($rel);exit;
    }
    /*
     *
     */
    public function token(){
        $token   =  I('post.token');
        $bizType =  I('post.type');
        $url     =  I('post.url');
        $api     = new \Common\Extend\Api\ApiClient();
        $api     = $api->credit();
        //$token = 'f2661621ac324b4e976e2449a84e706d';
        //$bizType = 'education';
        $api->method    = 'api.common.getStatus';
        $api->token     = $token;
        $api->bizType   = $bizType;
        $data=$api->Send('post','json')->data();

        //print_r($data);exit;
        if(empty($data['msg'])){
          ob_clean();
            $this->ajaxReturn(['status'=>false,'afresh'=>true,'info'=>'正在受权中，请稍后...']);
        }elseif($data['code']=='0100'){
            session('APPToken',$data['token']);
          ob_clean();
            $this->ajaxReturn(['status'=>true,'afresh'=>false,'info'=>'授权成功!','url'=>$url]);
        }else{
          ob_clean();
            $this->ajaxReturn(['status'=>false,'afresh'=>false,'info'=>$data['msg']]);
        }
    }
    /*
 *
 */

    public function mobiletoken(){
        $token   =  I('post.token');
        $url     =  I('post.url');
        $api     = new \Common\Extend\Api\ApiClient();
        $api     = $api->mobile();
        $api->method    = 'mobile_report/v1/task/status';
        $api->token     = $token;
        $data=$api->Send('post','json')->data();
        if(empty($data['msg'])){
            ob_clean();
            $this->ajaxReturn(['status'=>false,'afresh'=>true,'info'=>'正在受权中，请稍后...']);
        }elseif($data['code']=='0100'){
            session('APPToken',$data['token']);
            ob_clean();
            $this->ajaxReturn(['status'=>true,'afresh'=>false,'info'=>'授权成功!','url'=>$url]);
        }elseif($data['code']=='0006'){ //输入验证码
          ob_clean();
            $this->ajaxReturn(['status'=>false,'token'=>$data['token'],'captcha'=>true,'afresh'=>false,'info'=>'请输入验证码']);
        }else{
          ob_clean();
            $this->ajaxReturn(['status'=>false,'afresh'=>false,'info'=>$data['msg']]);
        }
    }
}



new RouterController();

